home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14220 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  78 lines

  1. Path: hydra.acs.uci.edu!awu
  2. From: awu@hydra.acs.uci.edu (Alex Wu)
  3. Newsgroups: comp.lang.c
  4. Subject: Simulating a Memory Manager
  5. Date: 12 Apr 1996 11:05:51 GMT
  6. Organization: University of California, Irvine
  7. Message-ID: <4kldef$png@news.service.uci.edu>
  8. NNTP-Posting-Host: hydra.acs.uci.edu
  9.  
  10. Hi, maybe you guys can help me, Im having problems moving around in memory.
  11.  
  12.   Okay Im suppose to write a mock-memory manager, that keeps tracks of
  13. a list of pointers that points to memory blocks are in use, and a list
  14. of open 'holes' in the memory.  At the beginning of each block, there
  15. is a field for tag (nominate if it's full or open)  and size (# of bytes
  16. in use).
  17.  
  18.  
  19. char *blocks[100];    /* keeps tracks of pointers that points to the
  20.              used blocks */
  21.  
  22. struct template
  23. {
  24.   short tag;
  25.   short size;
  26. };
  27.  
  28. char MM[32768];        /* size of the memory block we are keeping */
  29. char *MEM_PTR = MM;    /* points to the beginning of the block */
  30.  
  31.  
  32.  Okay, we first initialize the memory, so it's just one big hole.
  33.  
  34. struct template *HEAD
  35.  
  36. init_mem()
  37. {
  38.   HEAD->tag = TRUE;     /* it's a hole */
  39.   HEAD->size = 32768;
  40. }
  41.  
  42.  
  43.   Now it has to handle requests.  n is the number of bytes that I have to
  44. set aside.  Once I set it aside, I have to return the begining of the block.
  45. And of course adjust any open holes.
  46.  
  47. char *MM_Request(int n)
  48. {
  49.   struct template *s;
  50.  
  51.   s = HEAD;
  52.   if (s->size >= n) {    /* have the space, allocate it  */
  53.      
  54.     HEAD = s+n;          /* moves down n byte to mark the new location of
  55.                     the open block for next request ?????? */ 
  56.     HEAD->size = s->size - n;   /* remaining open mem size */
  57.     HEAD->tag = TRUE;           /* its a hole */
  58.     return (char *) s;       /* returns the head of the request byte */
  59.   }
  60. }
  61.  
  62. main ()
  63. {
  64.   loop {
  65.     blocks[counter] = MM_request(random bytes);
  66.   }
  67. }
  68.  
  69.  
  70. Now the program goes okay until it goes over 10k bytes.  any suggestions?
  71.  
  72.  
  73. Alex 
  74.  
  75.  
  76.  
  77.  
  78.